#pragma once 

#include "BasicType.h"
#include <string.h>

namespace LightPlayer{

	class CStrTokenIterator
	{
	public:
		explicit CStrTokenIterator(const char* i_pSrcStr, const char* i_separators = " \t\n\r")
		{
			m_pSrcStr = i_pSrcStr;
			strcpy_s(m_separators, (i_separators!=NULL) ? i_separators : "");
			Reset();
		}

		CStrTokenIterator(const char* i_pSrcStr, const char i_separator)
		{
			m_pSrcStr = i_pSrcStr;
			m_separators[0] = i_separator;
			m_separators[1] = '\0';
			Reset();
		}

		void Next()
		{
			m_bEnd = (*m_rIt=='\0');
			char* wIt = m_szToken; 
			if ( ! m_bEnd )
			{
				while (*m_rIt!='\0' && !IsSeparator(*m_rIt))
				{
					*wIt++ = *m_rIt++;
				}
				while (IsSeparator(*m_rIt)) ++m_rIt;
			}
			*wIt = '\0';
		}

		void Reset()
		{
			m_rIt = m_pSrcStr;
			m_szToken[0] = '\0';
			m_bEnd = false;
			while (IsSeparator(*m_rIt)) ++m_rIt;
			Next();
		}

		const char*		GetToken()	const	{ return m_szToken; }

		bool			IsEnd()		const	{ return m_bEnd;	}

	private:

		bool IsSeparator(const char i_char) const 
		{ 
			const char* it = m_separators;
			while (*it!='\0' && *it!=i_char) ++it;
			return (*it!='\0'); 
		}

		const char* m_pSrcStr;
		const char* m_rIt;
		bool m_bEnd;
		char m_separators[16];
		char m_szToken[512];
	};

};